﻿
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    public class Actor : ICollider
    {
        const int FRAMESPERSECOND = 20;
        public Vector2 position;
        public Vector2 direction;
        public Texture2D tex;
        public bool ignoreGravity = false;
        String mCurrentAnimation;
        int mCantAnimations =0;
        int mHframe;
        int mVframe;
        int mFrameWidth;
        int mFrameHeigth;
        float elapsedtime;
        float delta;
        int colissiongroup;
        public bool readyToexit = false;

        List<Animation> Animations = new List<Animation>();

        public Actor(ContentManager content,String texture) {
            tex = content.Load<Texture2D>(texture);
            delta = 1 / FRAMESPERSECOND * 1000;
           // direction.X = 1;
           // direction.Y = 1;
        }

        public void update(int dTime)
        {
            position += direction;//Posicion final sea igual a posicion inicial + direccion

            if (position.X + mFrameWidth > 800 || position.X < 0)
            {
                direction.X *= -1;
            }

            if (position.Y + mFrameHeigth > 600 || position.Y < 0)
            {
                direction.Y *= -1;
            }

            if (!ignoreGravity)
            {
                direction -= Physics.Gravity;
            }

            elapsedtime += dTime;
            if (elapsedtime >= delta)
            {
                GetCurrentAnimation().nextFrame();
                elapsedtime -= delta;
            }

        }

        public void Draw(SpriteBatch sb)
        {

            if (mCantAnimations > 0)
            {
                Animation a = GetCurrentAnimation();
                //Vector2 temp1 = a.getUVCoordinates(0);
                //Vector2 temp2 = a.getUVCoordinates(3);

                //Rectangle r = new Rectangle((int)temp1.X, (int)temp1.Y, (int)temp2.X, (int)temp2.Y);
                //vertex[0].textinfo = mAnimations[mCurrentAnimation]->getUVCoordinates(0);
                //vertex[1].textinfo = mAnimations[mCurrentAnimation]->getUVCoordinates(1);
                //vertex[2].textinfo = mAnimations[mCurrentAnimation]->getUVCoordinates(2);
                //vertex[3].textinfo = mAnimations[mCurrentAnimation]->getUVCoordinates(3);
                //sb.Draw(tex, position, r, Color.White);
                sb.Draw(tex, position, a.GetUVRectangle(), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
            }
            else
            {
                sb.Draw(tex, position, Color.White);
            }
            //g->Draw(vertex, rotation, position, texture);
            
            
            //sb.Draw(tex, position, r, Color.White);
        }

        public Animation GetCurrentAnimation()
        {
            foreach (Animation a in Animations)
            {
                if (a.Name == mCurrentAnimation) return a;
            }
            return null;
        }

        public void SetAnimation(String anim)
        {
            mCurrentAnimation = anim;
        }

        public void SetFrames(int hframe, int vframe)
        {
            mHframe = hframe;
            mVframe = vframe;
            mFrameWidth = tex.Width / mHframe;
            mFrameHeigth = tex.Height / mVframe;
        }

        public void SetDirection(float hdir, float vdir)
        {
            direction.X = hdir;
            direction.Y = vdir;
        }

        public void SetDirection(Vector2 dir)
        {
            direction.X = dir.X;
            direction.Y = dir.Y;
        }
        public void SetPosition(float hpos, float ypos)
        {
            position.X = hpos;
            position.Y = ypos;
        }

        public void SetPosition(Vector2 pos)
        {
            position.X = pos.X;
            position.Y = pos.Y;
        }


        public void CreateAnimation(String name, int [] frames)
        {
            Animation a = new Animation(name, tex.Height, tex.Width, mHframe, mVframe, frames);
            Animations.Add(a);
            mCantAnimations++;
        }


        //int GetGroup();
       // void SetGroup(int g);
        public void onColision() {
            readyToexit = true;
        }
        public Vector2 GetPosition()
        {
            return position;
        }
        public int GetWidth()
        {
            return mFrameWidth;
        }
        public int GetHeight()
        {
            return mFrameHeigth;
        }
    }
}
